<?php

namespace Shared;

/**
 * Description of Saver
 *
 * @author nofear
 */
abstract class Service {
    
//    abstract public function execute();
    
    /**
     * @return \Doctrine\ORM\EntityManager
     */
    protected $EntityManager;

    /**
     * @var \Symfony\Component\HttpFoundation\Request
     */
    protected $Request;
    
    public function __construct() {
        if (!Application::getInstance()->getCurrentUser()) {
            Application::getInstance()->createException('access denied!', 403);
            exit;
        }
        $this->EntityManager = \Shared\Application::getInstance()->getEntityManager();
        $this->Request = \Shared\Application::getInstance()->getRequest();
    }

    /**
     * 
     * @return \Symfony\Component\HttpFoundation\Request
     */
    public function getRequest() {
        $Request = \Shared\Application::getInstance()->getRequest();
        return $Request;
    }

    /**
     * 
     * @return \System\Session
     */
    public function getSession() {
        $Session = \Shared\Application::getInstance()->getSession();
        return $Session;
    }

    public function sanitize($str, $type = 'string') {
        switch ($type) {
            case 'email':
                $filter = FILTER_SANITIZE_EMAIL;
                break;
            case 'int':
                $filter = FILTER_SANITIZE_NUMBER_INT;
                break;
            default:
                $filter = FILTER_SANITIZE_STRING;
                break;
        }

        $str = filter_var(strip_tags(trim($str)), $filter);

        return $str;
    }

    public function redirect($url) {
        header('Location: ' . $url, 301);
        exit();
    }
}
